﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Argos.Collections
{
	/// <summary>
	/// A list that contains only unique items where one item does not
	/// equal any other item in the list.
	/// </summary>
	/// <remarks>
	/// This list does not throw an exception if a duplicate item is added to
	/// the collection. That item is simply not added to the list.
	/// </remarks>
	/// <typeparam name="T">The type of items to hold in the list.</typeparam>
	[Serializable]
	public class UniqueList<T> : EventfulList<T>
	{

		#region #ctor: public UniqueList()

		/// <summary>
		/// Creates a new instance of the class.
		/// </summary>
		public UniqueList() { }

		#endregion
		
		#region #ctor: public UniqueList(IEnumerable<T> collection)

		/// <summary>
		/// Creates a new instance from the given collection.
		/// </summary>
		/// <param name="collection">A collection containing the items for the new instance.</param>
		public UniqueList(IEnumerable<T> collection) : base(collection) { }

		#endregion



		#region protected override void OnItemAdding(CancelItemEventArgs<T> e)

		/// <summary>
		/// Checks that the item being added does not exist in the list.
		/// </summary>
		protected override void OnItemAdding(CancelItemEventArgs<T> e)
		{
			if (this.Contains(e.Item))
			{
				e.Cancel = true;
			}
		}

		#endregion
		
		#region protected override void OnItemSetting(CancelItemEventArgs<T> e)
				
		/// <summary>
		/// Checks that the item being set does not exist in the collection.
		/// </summary>
		protected override void OnItemSetting(CancelItemEventArgs<T> e)
		{
			if (this.Contains(e.Item))
			{
				e.Cancel = true;
			}
		}

		#endregion

	}
}
